package com.example.controller;

import com.example.entity.*;
import com.example.service.IGouWuChe;
import com.example.service.IProductService;
import com.example.service.IProductTypeService;
import com.example.service.IShoucangService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/product")

public class ProductController {

    @Autowired
    private IGouWuChe gouWuChe;
    @Autowired
    private IProductService productService;

    @Autowired
    private IProductTypeService productTypeService;

    @Autowired
    private IShoucangService shoucangService;


    @GetMapping("/index")
    public String index_assn(){
        return "product/index";
    }

    @RequestMapping("/page_content")
    public String index(String name, Integer typeId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "12") Integer pageSize, HttpServletRequest request)  {


        PageInfo<Goods> pageInfo = productService.findAll(name,typeId,pageNum,pageSize);

        request.setAttribute("pageInfo",pageInfo);

        //产品类型
        List<GoodsType> typeList = productTypeService.findAll();

        request.setAttribute("typeList",typeList);

        //数据的回显
        request.setAttribute("typeId",typeId);

        request.setAttribute("name",name);
        return "common/page_content";
    }

    @GetMapping("/productdetail")
    public String product_detail(Integer goodsId, HttpSession session){

       Goods goods= productService.findOneByProductId(goodsId);
        session.setAttribute("goods",goods);

        return "common/productdetail";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(String goodsId){
        int ID=Integer.valueOf(goodsId);
        Goods goods = gouWuChe.good(ID);
        String name = goods.getGoodsName();
        double price = goods.getGoodsPrice();
        String img = goods.getGoodsImgs();
        String remakr = goods.getGoodsRemark();
        Gouwuche gwd = new Gouwuche(0,name,price,img,1,remakr);
        int i = gouWuChe.InsertGood(gwd);
        if(i==0){
            return new Result("500","提交失败",null);
        }else{
            return new Result("200","提交成功",null);
        }


    }
    @RequestMapping("/insert2")
    @ResponseBody
    public Result insert2(String goodsId){
        int id = Integer.valueOf(goodsId);

        Shoucang sc = shoucangService.sc(id);
        Goods goods = gouWuChe.goodName(sc.getGoodsName());
        String name = goods.getGoodsName();
        double price = goods.getGoodsPrice();
        String img = goods.getGoodsImgs();
        String remakr = goods.getGoodsRemark();
        Gouwuche gwd = new Gouwuche(0,name,price,img,1,remakr);
        int i = gouWuChe.InsertGood(gwd);

        if(i==0){
            return new Result("500","提交失败",null);
        }else{
            return new Result("200","提交成功",null);
        }
    }
    @RequestMapping("/seller")
    public String seller(String userId,HttpServletRequest request){

        return "product/seller";
    }

    @RequestMapping("/page_mai")
    public String toPage_mai(String userId,HttpServletRequest request){
        int id = Integer.valueOf(userId);
        List<Goods> UserGoods = productService.findUserGoods(id);
        request.setAttribute("UserGoodsList",UserGoods);
        return "product/page_mai";
    }
}
